﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using SalesSys.WebCore.Authorize;

namespace SalesSys.WebCore
{
   public static class HtmlEx
    {
       /// <summary>
       /// 根据数据类型自动选择输入框类型
       /// </summary>
       /// <typeparam name="TModel"></typeparam>
       /// <typeparam name="TProperty"></typeparam>
       /// <param name="htmlHelper"></param>
       /// <param name="expression"></param>
       /// <param name="htmlAttributes"></param>
       /// <returns></returns>
       public static MvcHtmlString InputFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes=null)
       {
           var dic = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
           return InputFor(htmlHelper, expression, dic);
       }
       public static MvcHtmlString InputFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
       {
         var met= ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
           var type=met.ModelType;

         if (met.ModelType==typeof(DateTime)||met.ModelType==typeof(DateTime?))
         {
             htmlAttributes["type"] = "datetime";
             return htmlHelper.TextBoxFor(expression,format: "{0:yyyy-MM-dd HH:mm:ss}",htmlAttributes:  htmlAttributes);
         }
         else if (met.ModelType == typeof(int) || met.ModelType == typeof(long) || met.ModelType == typeof(float) || met.ModelType == typeof(double) || (met.ModelType == typeof(int?) || met.ModelType == typeof(long?) || met.ModelType == typeof(float?) || met.ModelType == typeof(double?)))
         {
             htmlAttributes["type"] = "number";
         }
         else if (met.ModelType == typeof(bool) )
         {
             object exp = expression;
             return htmlHelper.CheckBoxFor((Expression<Func<TModel, bool>>)exp, htmlAttributes);
         } 
         else if (type.IsEnum)
         {
             if (met.Model == null)
             {
                 return htmlHelper.DropDownListFor(expression, EnumExt.ToSelectListItems(type), htmlAttributes);
             }
             return htmlHelper.DropDownListFor(expression, EnumExt.ToSelectListItems((Enum)met.Model), htmlAttributes);
         }
           return htmlHelper.TextBoxFor(expression, htmlAttributes);
       }

       /// <summary>
       /// 为页面添加一个时间戳标记，用于防止重复提交及页面过期，配合TimestampTokenAttribute标签使用
       /// </summary>
       /// <param name="htmlHelper"></param>
       /// <returns></returns>
       public static MvcHtmlString TimestampToken(this HtmlHelper htmlHelper)
       {
           var str = TimestampTokenAttribute.CreateToken(htmlHelper.ViewContext.RequestContext.HttpContext.Request.GetHashCode());
           str = string.Format("<input type='hidden' name='{0}' value='{1}'  />", TimestampTokenAttribute.FormToken, str);
           return new MvcHtmlString(str);
       }

    }
}
